<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>12 - 删除立方体的一个面</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
    </style>
    <!-- 引入三维引擎 -->
    <script src="../external/three-js-r93/build/three.js"></script>
    <!-- 扩展控件 OrbitControls -->
    <script src="../external/three-js-r93/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<script>

    let scene = new THREE.Scene();

    let boxGeometry = new THREE.BoxGeometry(50, 50, 50);
    boxGeometry.faces.pop();
    boxGeometry.faces.shift();

    // boxGeometry.faces.forEach(face => {
    //
    //     face.vertexColors = [
    //         new THREE.Color(0xff0000),
    //         new THREE.Color(0xffff00),
    //         new THREE.Color(0x00ffff)
    //     ]
    //
    // });

    /* 设置三角面 face 三个顶点的颜色 */
    boxGeometry.faces.forEach(function (face) {

        face.vertexColors = [
            new THREE.Color(0xff0000),
            new THREE.Color(0xffff00),
            new THREE.Color(0x00ffff)
        ]

    });

    let material = new THREE.MeshLambertMaterial({
        // color: 0xffff00
        vertexColors:THREE.VertexColors,
        side:THREE.DoubleSide
    });

    let box = new THREE.Mesh(boxGeometry, material);

    scene.add(box);


    /* 添加辅助坐标 */
    let axesHelper = new THREE.AxesHelper(200);
    scene.add(axesHelper);

    //点光源
    var point = new THREE.PointLight(0xffffff);
    point.position.set(400, 200, 300); //点光源位置
    scene.add(point); //点光源添加到场景中
    //环境光
    var ambient = new THREE.AmbientLight(0x444444);
    scene.add(ambient);

    /* 添加相机 */
    let width = window.innerWidth;
    let height = window.innerHeight;
    let k = width / height; // 窗口宽高比
    let s = 150; // 三维场景显示范围控制系数， 系数越大，显示的范围越大

    let camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
    camera.position.set(200, 300, 200);
    camera.lookAt(scene.position);

    /* 渲染器 */
    let renderer = new THREE.WebGLRenderer({antialis: true});// 抗锯齿
    renderer.setSize(width, height);//渲染区域尺寸
    renderer.setClearColor(0xb9d3ff, 1);//渲染背景色

    document.body.appendChild(renderer.domElement); // body 元素中插入 canvas 对象

    /*渲染函数*/
    function render() {

        renderer.render(scene, camera);

    }

    render();

    /* 添加控件对象 */
    let controls = new THREE.OrbitControls(camera, renderer.domElement);// 控件可以监听鼠标的变化，改变相机的属性
    controls.addEventListener('change', render);//添加鼠标监听，触发渲染函数，更新 canvas 画布渲染效果

</script>

</body>
</html>